package com.dfrz.problem.service.impl;

import java.util.List;

import com.dfrz.problem.dao.EmployeeDao;
import com.dfrz.problem.model.Employee;
import com.dfrz.problem.service.EmployeeService;
import com.dfrz.problem.utils.IConstant;
import com.dfrz.problem.utils.MD5Util;
import com.dfrz.problem.utils.Result;

public class EmployeeServiceImpl implements EmployeeService {
	private EmployeeDao employeeDao;
	
	@Override
	public Result<Employee> updateEmployee(Employee employee) {
		Result<Employee> result = new Result<Employee>();
		if(employeeDao.updateEmployee(employee)){
			result.setCode(IConstant.UPDATE_SUCCESS);
		}else{
			result.setCode(IConstant.UPDATE_LOSE);
		}
		result.setResult(employee);
		
		return result;
	}

	public void setEmployeeDao(EmployeeDao employeeDao) {
		this.employeeDao = employeeDao;
	}

	@Override
	public boolean checkPwd(String password, String email) {
		return employeeDao.checkPwd(MD5Util.getMD5Str(password),email);
	}

	@Override
	public boolean updatePwd(String email, String password) {
		return employeeDao.updatePwd(email,MD5Util.getMD5Str(password));
	}

	@Override
	public Result<Employee> getEmployee(String email) {
		Result<Employee> result = new Result<Employee>();
		
		if(email.trim().length() == 0){
			result.setCode(IConstant.NOT_LOGIN);
			return result;
		}
		
		result.setResult(employeeDao.getEmployee(email));
		
		return result;
	}

	@Override
	public Result<Employee> getEmployeeOnProblemId(Integer problemId,Integer roleId) {
		Result<Employee> result = new Result<Employee>();
		Employee employee = employeeDao.getEmployeeOnProblemId(problemId,roleId);
		if(employee == null){
			result.setCode(IConstant.EMPLOYEE_NOT_EXIST);
			return result;
		}
		result.setResult(employee);
		
		return result;
	}

	@Override
	public Result<List<Employee>> getListEmployee(Integer projectId) {
		Result<List<Employee>> result = new Result<List<Employee>>();
		List<Employee> list = employeeDao.getListEmployee(projectId);
		if(list == null || list.size() == 0){
			result.setCode(IConstant.EMPLOYEE_NOT_EXIST);
			return result;
		}
		result.setResult(list);
		
		return result;
	}

	@Override
	public Result<List<Employee>> getNotAssign() {
		Result<List<Employee>> result = new Result<List<Employee>>();
		result.setResult(employeeDao.getListEmployee(1));
		
		return result;
	}

	@Override
	public Result<Employee> getEmployee(Integer employeeId) {
		Result<Employee> result = new Result<Employee>();
		Employee employee = employeeDao.getEmployee(employeeId);
		if(employee != null){
			result.setResult(employee);
		}else{
			result.setCode(IConstant.EMPLOYEE_NOT_EXIST);
		}
		
		return result;
	}
	
	@Override
	public Result<List<Employee>> getEmployees() {
		Result<List<Employee>> result = new Result<List<Employee>>();
		List<Employee> list = employeeDao.getEmployeeAll();
		if(list == null || list.size() == 0){
			result.setCode(IConstant.EMPLOYEE_NOT_EXIST);
			return result;
		}
		result.setResult(list);
	
		return result;
	}

	@Override
	public Result<List<Employee>> getAllEmployee() {
		Result<List<Employee>> result = new Result<List<Employee>>();
		result.setResult(employeeDao.getEmployeeAll());
		return result;
	}

	@Override
	public void updateEmployeeProject(Employee employee) {
		employeeDao.updateEmployeeProject(employee);
	}
}
